package com.msnjob.base;

import java.io.File;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;
import java.util.Vector;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import base.db.lib.DBHelper;
import base.db.lib.util.ConncenFactory;
import base.image.lib.CompressPic;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * Show case File Upload example's action. <code>FileUploadAction</code>
 * 
 */
@SuppressWarnings("all")
public class FileUploadAction extends ActionSupport {

	private static final long serialVersionUID = 5156288255337069381L;

	private String contentType;
	private File upload;
	private String fileName;
	public String getUploadFileName() {
		return fileName;
	}
	public void setUploadFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getUploadContentType() {
		return contentType;
	}
	public void setUploadContentType(String contentType) {
		this.contentType = contentType;
	}
	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String input() throws Exception {
		return SUCCESS;
	}

	// <c:set var="request" scope="page" value="${pageContext.request}" />
	// <c:set var="base" scope="page"
	// value="${request.scheme}://${request.serverName}:${request.serverPort}"
	// />
	// <c:set var="contextPath" scope="page" value="${request.contextPath}" />
	// <c:set var="basePath" scope="page" value="${base}${contextPath}" />
	public String upload() throws Exception {
		// File htmlFile = new File(savePath + extName);
		Calendar calendar = Calendar.getInstance();// 获得初始化的时间类；
		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac
				.get(ServletActionContext.SERVLET_CONTEXT);
		String savePath = sc.getRealPath("/");
		savePath = savePath + "upload\\test\\";
		String filename_date = String.valueOf(calendar.getTimeInMillis());// 将时间的毫秒作为文件名字的字符串
		File file = new File(savePath + filename_date + fileName);
		// 复制文件
		FileUtils.copyFile(upload, file);
		System.out.println(fileName);

		HttpServletRequest request = ServletActionContext.getRequest();
		PrintWriter out = ServletActionContext.getResponse().getWriter();
		System.out.println("ssssssssssssssssssssss");
		// 获得requst中的文件输入流对象把文件写到服务器WebRoot/upload"

		String url = "" + request.getScheme() + "://" + request.getServerName()
				+ ":" + request.getServerPort() + request.getContextPath();
		url = url + "/upload/test/" + filename_date + fileName;
		System.out.println(url);
		System.out.println(upload.getPath());
		System.out.println(file.getPath());
		
		
		
		CompressPic mypic = new CompressPic(); 
		    //源文件路径   存储路径  新文件名   
 		mypic.doCompressPic(file.getPath(), "f:\\test\\", "r14444"+55+fileName, 120, 120, true); 
 
		// JSONObject json_object = new JSONObject();
		// json_object.put("done",0);
		// json_object.put("url",url);
		// json_object.put("width",120);
		// String script = "try{parent.nicUploadButton.statusCb('" +
		// json_object.toString() +
		// "')}catch(e){alert(parent);alert(e.message); }";
		// out.println("<script>" + script + "</script>");
		// System.out.println(script);

		return null;
	}
	/**
	 * 处理上传图片
	 * @return
	 * @throws Exception
	 */
	public String doPic() throws Exception {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
		String savePath = sc.getRealPath("/");
		savePath = savePath + "/uploadfile/photo/";
          String filename_date=sdf.format(new Date());
		File file = new File(savePath + filename_date +"temp"+ fileName.substring(fileName.lastIndexOf("."), fileName.length()).toLowerCase());
		// 复制文件
		FileUtils.copyFile(upload, file);
		HttpServletRequest request = ServletActionContext.getRequest();
	   //PrintWriter out = ServletActionContext.getResponse().getWriter();
		//图片文件名
		String picFilename=filename_date+fileName.substring(fileName.lastIndexOf("."), fileName.length()).toLowerCase();
//		String url = "" + request.getScheme() + "://" + request.getServerName()
//		+ ":" + request.getServerPort() + request.getContextPath();
//		url = url + "/uploadfile/photo/" + picFilename;
//		System.out.println(url);
		CompressPic mypic = new CompressPic(); 
		 //源文件路径   存储路径  新文件名   
 		mypic.doCompressPic(file.getPath(), savePath, picFilename, 240, 320, true); 
        // 上传老图片
 		file.delete();
 		Map userInfo=this.getUserInfo();
		if(userInfo !=null){
		Vector params = new Vector();
		params.add("1");
		params.add("1");
		params.add(picFilename);
		params.add((String)userInfo.get("username"));
		params.add((String)userInfo.get("jobid"));
		//{phototre=1, photopb=1, username=200688, name=王建勇, photo=2006251055.jpg, jobid=pqdl91njf0}
		StringBuffer sql=new StringBuffer("update person  set phototre=?,photopb=?,photo=?  where username=? and jobid=?");
		 DBHelper.execute(sql.toString(), params);
		ConncenFactory.commitAndClose(); // 关闭连接    // 关闭连接
		}
 		ServletActionContext.getResponse().sendRedirect("user/person!goToPic.do");
		return null;
	}
		//获取用户登录信息
		public Map getUserInfo(){
			HttpServletRequest request = ServletActionContext.getRequest();
			//清理会话
		   return (Map)request.getSession().getAttribute("user");
		}

}
